package com.bdqn.rabbitmqporducer1.controller;

import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class SendMsgController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendDirectMsg")
    public String sendDirectMsg() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "This is My Direct Message!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("MyDirectExchange", "MyDirectRouting", map);
        return "消息发送成功！";
    }

    @GetMapping("/sendFanoutMsg")
    public String sendFanoutMsg() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "This is My Fanout Message!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("fanoutExchange", null, map);
        return "消息发送成功！";
    }

    @GetMapping("/sendTopicMsg1")
    public String sendTopicMessage1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "This is My Topic Message!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> womanMap = new HashMap<>();
        womanMap.put("messageId", messageId);
        womanMap.put("messageData", messageData);
        womanMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.First", womanMap);
        return "消息发送成功！";
    }

    @GetMapping("/sendTopicMsg2")
    public String sendTopicMessage2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "This is My Topic Message!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> manMap = new HashMap<>();
        manMap.put("messageId", messageId);
        manMap.put("messageData", messageData);
        manMap.put("createTime", createTime);
        rabbitTemplate.convertAndSend("topicExchange", "topic.Second", manMap);
        return "消息发送成功！";
    }

    @GetMapping("/msgAck1")
    public String msgAck1() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "this is exchange_null message";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("exchange_null", "MyDirectRouting", map);
        return "消息发送成功！";
    }

    @GetMapping("/msgAck2")
    public String msgAck2() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "this is AloneDirectExchange message";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messageData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("AloneDirectExchange", "MyDirectRouting", map);
        return "消息发送成功！";
    }

    @GetMapping("/sendConfirmMsg")
    public String sendConfirmMsg() {
        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "this is Confirm message";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>();
        map.put("messageId", messageId);
        map.put("messagData", messageData);
        map.put("createTime", createTime);
        rabbitTemplate.convertAndSend("ConfirmExchange", "ConfirmRouting", map);
        return "消息发送成功！";
    }
}
